import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/controller/mine/address_controller.dart';
import 'package:guochaoapp/model/address_entity.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCAddAddressController extends GetxController {
  UserService userService = Get.find();

  var isEdit = false;
  AddressDataRecord model;

  var receiverAddr = "上海市静安区".obs;
  var receiverName = "".obs;
  var receiverPhone = "".obs;
  var province = "上海".obs;

  locationTap() {}

  submitTap() {

    if(isEdit) {
      HttpClient.request(
          url: UserAddrApi.usersAddr,
          method: Method.PUT,
          data: {
            "isDefault" : true,
            "uid": userService.userInfo.uid,
            "province": province.value,
            "receiverAddr": receiverAddr.value,
            "receiverName": receiverName.value,
            "receiverPhone": receiverPhone.value,
            "addrId": model.addrId
          },
          onSuccess: (r, _) {

            GCAddressController ad_controller = Get.find();
            ad_controller.er_controller.callRefresh();
            Get.back();
          },
          onFailed: (err) {
            EasyLoading.showError(err);
          });
    } else {
      HttpClient.request(
          url: UserAddrApi.usersAddr,
          method: Method.POST,
          data: {
            "isDefault" : true,
            "uid": userService.userInfo.uid,
            "province": province.value,
            "receiverAddr": receiverAddr.value,
            "receiverName": receiverName.value,
            "receiverPhone": receiverPhone.value,
          },
          onSuccess: (r, _) {

            GCAddressController ad_controller = Get.find();
            ad_controller.er_controller.callRefresh();
            Get.back();
          },
          onFailed: (err) {
            EasyLoading.showError(err);
          });
    }


  }
}
